//
//  FDLoginController.m
//  FamousDoctorOnline
//
//  Created by wszyxc on 16/7/6.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDLoginController.h"
#import "WZHUserDefaults.h"
#import "JASidePanelController.h"
#import "FDLeftController.h"

#import <SVProgressHUD.h>

@interface FDLoginController ()

@property (weak,nonatomic) UIImageView * iconImageView;

@property (weak,nonatomic) UIImageView * titleImageView;

@property (weak,nonatomic) UITextField * nameField;

@property (weak,nonatomic) UITextField * passwordField;

@property (weak,nonatomic) UIButton * loginBtn;

@end





@implementation FDLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setupLayout];

}

- (void)setupUI{
    
    //添加图片视图
    UIImageView *iconImageView = [[UIImageView alloc]init];
    
    self.iconImageView = iconImageView;
    
    iconImageView.image = [UIImage imageNamed:@"product_img_logo_nor.png"];
    
    [self.view addSubview:iconImageView];
    
    
    //添加标题imageView
    UIImageView *titleImageView = [[UIImageView alloc]init];
    
    self.titleImageView = titleImageView;
    
    titleImageView.image = [UIImage imageNamed:@"product_text_logo_nor.png"];
    
    [self.view addSubview:titleImageView];
    
    //添加nameField
    
    UITextField *nameField = [[UITextField alloc]init];
    
    nameField.placeholder =@"请输入用户名";
    
    nameField.background = [UIImage imageNamed:@"shurukuang"];
    
    nameField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    
    nameField.leftViewMode = UITextFieldViewModeAlways;
    
    [self.view addSubview:nameField];
    
    self.nameField = nameField;
    
    [nameField addTarget:self action:@selector(fieldEditChange) forControlEvents:UIControlEventEditingChanged];
    
    //添加passwordField
    
    UITextField *passwordField = [[UITextField alloc]init];
    
    passwordField.placeholder = @"请输入密码";
    
    passwordField.background = [UIImage imageNamed:@"shurukuang"];
    
    passwordField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    
    passwordField.leftViewMode = UITextFieldViewModeAlways;
    
    [self.view addSubview:passwordField];
    
    self.passwordField = passwordField;
    
    [passwordField addTarget:self action:@selector(fieldEditChange) forControlEvents:UIControlEventEditingChanged];
    
    //添加登录按钮
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.loginBtn = loginBtn;
    
    [loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    
    [loginBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    
    [loginBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    
    [loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:loginBtn];
    
    loginBtn.enabled = NO;
    
}

- (void)fieldEditChange{
    
    NSString *userName = self.nameField.text;
    NSString *passWord = self.passwordField.text;
    
    //如果都有值
    if ([userName isEqualToString:@""]||[passWord isEqualToString:@""]) {
       // NSLog(@"有空值");
        self.loginBtn.enabled = NO;
    }else{
      //  NSLog(@"都有值");
        self.loginBtn.enabled = YES;
    }
    
    
}


     
/**
 *  点击登陆
 */
- (void)loginClick{
   
    //点击登陆,如果密码正确,就进入登陆状态,保存登陆状态
 
    NSArray *userInfo = [WZHUserDefaults WZHObjectForKey:@"userInfo"];
    
    //取出用户名和密码
    NSString *getName = userInfo[kUserInfoUserName];
    
    NSString *getPassword = [WZHUserDefaults WZHStringForKey:@"password"];
//    userInfo[kUserInfoPassWord];
    
    //如果用户名和密码取出的有值
    if (getName&&getPassword) {
      //  NSLog(@"本地已有数据");
        
        //如果输入的用户名密码正确
        if ([getName isEqualToString:self.nameField.text] && [getPassword isEqualToString:self.passwordField.text]) {
            
            //修改leftVC的用户资料内容为以登陆状态
           JASidePanelController *centerVC = (JASidePanelController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            
            FDLeftController *leftVC = (FDLeftController *)centerVC.leftPanel;
            
            leftVC.isLogin = YES;
            
            //保存已登陆状态
           [WZHUserDefaults WZHSaveBoolValue:YES forKey:@"isLogin"];
            
            //跳回
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:@"用户名或密码错误!"];
            
            [self performSelector:@selector(dismissSV) withObject:nil afterDelay:2];
            
        }
        
    }else{
        
        [SVProgressHUD showInfoWithStatus:@"未设置用户名和密码"];
        
        [self performSelector:@selector(dismissSV) withObject:nil afterDelay:1];
    
        [self.navigationController popViewControllerAnimated:YES];
//        [WZHUserDefaults WZHSaveObject:@"FD" forKey:@"userName"];
//        
//        [WZHUserDefaults WZHSaveObject:@"FD" forKey:@"passWord"];
        
        
        
    }
    
    
}

- (void)dismissSV{
    
    [SVProgressHUD dismiss];
    
}



- (void)setupLayout{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.offset(100);
        make.width.offset(67);
        make.height.offset(88);
        
    }];
    
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.iconImageView.mas_bottom).offset(0);
        make.width.offset(83);
        make.height.offset(49);
        
    }];
    
    [self.nameField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.titleImageView.mas_bottom).offset(20);
        make.leading.offset(10);
        make.trailing.offset(-10);
        make.height.offset(30);
        
        
    }];
    
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.offset(10);
        make.trailing.offset(-10);
        make.top.equalTo(self.nameField.mas_bottom).offset(20);
        make.height.offset(30);
        
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.offset(10);
        make.trailing.offset(-10);
        make.top.equalTo(self.passwordField.mas_bottom).offset(35);
        make.height.offset(45);
        
    }];
    
    
}


//键盘回退

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
    
}






@end
